package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class SpotCategoryPeer extends BasePeer{
	public static final String tableName = "_Spot_Category";
	public static final String APPID = "appId";
	public static final String SPOTID = "spotId";
	public static final String CATEGORYID = "categoryId";
	public static final String[] columnNames=new String[]{"appId","spotId","categoryId"};
	public static SpotCategory getSpotCategoryByPKs(Object appId,Object categoryId,Object spotId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(SpotCategoryPeer.tableName,SpotCategoryPeer.columnNames," TRUE  AND `appId`=?  AND `categoryId`=?  AND `spotId`=? ",new Object[]{appId,categoryId,spotId,},0, 1,conn);
			SpotCategory spotCategory = new SpotCategory();
		fill(rs,spotCategory);
		return spotCategory;
	}
	public static SpotCategory getSpotCategoryReferenceColumnsOnlyByPKs(Object appId,Object categoryId,Object spotId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(SpotCategoryPeer.tableName,new String[]{"appId","categoryId","spotId",}," TRUE AND `appId`=?AND `categoryId`=?AND `spotId`=?",new Object[]{appId,categoryId,spotId,}, 0,  1,conn);
			SpotCategory spotCategory = new SpotCategory();
		fill(rs,spotCategory);
		return spotCategory;
	}
	public static SpotCategory getSpotCategoryByAppId(long appId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(SpotCategoryPeer.tableName,SpotCategoryPeer.columnNames,"`appId`=?",new Object[]{appId},0, 1,conn);
			SpotCategory spotCategory = new SpotCategory();
		fill(rs,spotCategory);
		return spotCategory;
	}
	public static SpotCategory getSpotCategoryBySpotId(long spotId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(SpotCategoryPeer.tableName,SpotCategoryPeer.columnNames,"`spotId`=?",new Object[]{spotId},0, 1,conn);
			SpotCategory spotCategory = new SpotCategory();
		fill(rs,spotCategory);
		return spotCategory;
	}
	public static SpotCategory getSpotCategoryByCategoryId(int categoryId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(SpotCategoryPeer.tableName,SpotCategoryPeer.columnNames,"`categoryId`=?",new Object[]{categoryId},0, 1,conn);
			SpotCategory spotCategory = new SpotCategory();
		fill(rs,spotCategory);
		return spotCategory;
	}
	public static void fill(ResultSet rs, SpotCategory spotCategory) throws SQLException {
		if(rs.next()){
			try{spotCategory.setAppId(rs.getLong("appId"));}catch(Exception ex){}
			try{spotCategory.setSpotId(rs.getLong("spotId"));}catch(Exception ex){}
			try{spotCategory.setCategoryId(rs.getInt("categoryId"));}catch(Exception ex){}
			spotCategory.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<SpotCategory> spotCategoryCollection) throws SQLException {
		while(rs.next()){
			SpotCategory spotCategory=new SpotCategory();
			try{spotCategory.setAppId(rs.getLong("appId"));}catch(Exception ex){}
			try{spotCategory.setSpotId(rs.getLong("spotId"));}catch(Exception ex){}
			try{spotCategory.setCategoryId(rs.getInt("categoryId"));}catch(Exception ex){}
			spotCategory.resetModified();
			spotCategoryCollection.add(spotCategory);
		}
	}
	public static boolean update(SpotCategory spotCategory, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < SpotCategoryPeer.columnNames.length; i++) {
			if (((SpotCategoryBase)spotCategory).modified[i]) {
				columns.add(SpotCategoryPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `_Spot_Category` SET ");
		sql.append("WHERE TRUE AND `appId` = ? AND `categoryId` = ? AND `spotId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,spotCategory.get(column));
		}
		pstmt.setObject(columns.size()+1+0,spotCategory.get("appId"));
		pstmt.setObject(columns.size()+1+1,spotCategory.get("categoryId"));
		pstmt.setObject(columns.size()+1+2,spotCategory.get("spotId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(SpotCategory spotCategory, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (spotCategory.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `_Spot_Category` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (spotCategory.get(column) != null) {
				pstmt.setObject(i + 1 + j, spotCategory.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<SpotCategory> getSpotCategoryCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<SpotCategory> collection=new Vector<SpotCategory>();
		fill(rs,collection);
		return collection;
	}
}
